package com.sgb.merchant.widget;

import android.content.Context;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;

import com.sgb.link.entity.merchant.IndustryCategoryEntity;
import com.sgb.link.entity.merchant.IndustryType;
import com.sgb.merchant.R;
import com.sgb.merchant.databinding.MerchantPopwindowFilterTypeBinding;
import com.sgb.merchant.databinding.MerchantPopwindowTypeBinding;
import com.sgb.merchant.view.adapter.ItemTypeAdapter;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.util.MToast;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: 王彬臣
 * @date: 2021/9/17
 * @Description: 类型弹框支持单选和多选
 */
public class MultipleSelectTypePopWindow extends BasePopWindow implements View.OnClickListener {

    MerchantPopwindowTypeBinding mBinding;

    List<IndustryCategoryEntity> mDataList;
    ItemTypeAdapter mAdapter;
    String title="";

    List<IndustryCategoryEntity> mSelectList=new ArrayList<>();
    boolean isSingleSelect;

    public MultipleSelectTypePopWindow(Context context) {
        super(context, DIR_DOWN_UP);

    }

    public void setDataList(String title, List<IndustryCategoryEntity> mDataList) {
        this.mDataList = mDataList;
        this.title=title;
        mAdapter.setDatas(mDataList);
        mBinding.tvTitle.setText(title);

    }

    public void setSingleSelect(boolean singleSelect) {
        isSingleSelect = singleSelect;
        if (!isSingleSelect) {
            mAdapter.setMultiSelect(true);
            mAdapter.setMost(10);
            mAdapter.setSelectMsg("最多可选择10个");
            mAdapter.setOnItemMultiClick(new BaseRecycleAdapter.OnItemMultiClick<IndustryCategoryEntity>() {
                @Override
                public void onItemMultiClick(List<IndustryCategoryEntity> industryCategoryEntities, int position) {
                    mSelectList.clear();
                    mSelectList.addAll(industryCategoryEntities);
                }
            });
        } else {
            mAdapter.setItemSelect(true);
            mAdapter.setOnItemClick(new AdapterOnItemClick<IndustryCategoryEntity>() {
                @Override
                public void onItemClick(IndustryCategoryEntity entity, int position) {
                    mSelectList.clear();
                    mSelectList.add(entity);
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_re_cancel) {
            dismiss();
        } else if (id == R.id.tv_ok) {
            if(mSelectList.size()==0){
                MToast.showToast(mContext,String.format("请%s！",title));
                return;
            }
            if (onResultCallBack != null) {
                List<IndustryCategoryEntity> mSortList=new ArrayList<>();
                if(mSelectList.size()>1){//多选重排序
                    for(IndustryCategoryEntity mData:mDataList){
                        for(IndustryCategoryEntity industryCategoryEntity:mSelectList){
                            if(industryCategoryEntity.getCategoryNo().equals(mData.getCategoryNo())){
                                mSortList.add(mData);
                                break;
                            }
                        }

                    }
                    onResultCallBack.result(mSortList);
                }else{
                    onResultCallBack.result(mSelectList);
                }
                dismiss();
            }

        }

    }

    @Override
    protected int popLayout() {
        return R.layout.merchant_popwindow_type;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        mBinding = (MerchantPopwindowTypeBinding) holder.getmBinding();
        mBinding.setOnClick(this);
        mBinding.ryType.setLayoutManager(new GridLayoutManager(mContext, 4));
        mAdapter = new ItemTypeAdapter(mContext, null);
        mAdapter.setDefItem(-1);
        mBinding.ryType.setAdapter(mAdapter);


    }

}
